package com.example.satokendemo01.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    //登录
    @GetMapping("/doLogin")
    public SaResult doLogin(String name, String pwd) {

        if ("admin".equals(name) && "123".equals(pwd)) {
            StpUtil.login(10001);
            return SaResult.ok("登陆成功");
        }
        return SaResult.error("登录失败");
    }

    //登录并返回Token
    @GetMapping("/doLogin2")
    public SaResult doLogin2(String name, String pwd){
        if ("admin".equals(name) && "123".equals(pwd)) {
            StpUtil.login(10003);
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return SaResult.data(tokenInfo);
        }

        return SaResult.error("登录失败");
    }

    @GetMapping("/logout")
    public SaResult logout(){
        StpUtil.logout();
        return SaResult.ok("退出成功");
    }

    //获取用户信息
    @GetMapping("/userInfo")
    public Object userInfo(){

        return SaResult.ok()
                .set("id",StpUtil.getLoginId())
                .set("name","suyan")
                .set("sex","男")
                .set("age",18);

    }


}
